package com.fire.control.system.module.file.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.fire.control.system.dto.app.AppFileVo;
import com.fire.control.system.entity.File;
import com.fire.control.system.module.file.dao.FileRepository;
import com.fire.control.system.module.file.service.AppFileService;
import com.fire.control.system.statics.ResponseMessage;
import com.fire.control.system.support.protocol.app.AppRequestProtocol;
import com.fire.control.system.support.protocol.comman.ProtocolSelector;
import com.fire.control.system.support.protocol.comman.ResponseProtocol;
import com.fire.control.system.support.util.FileUtil;
import com.fire.control.system.support.util.XdaoDate;

/**
 * 
 * @ClassName:  AppFileServiceImpl
 * @Description:  文件处理接口具体实现
 * @author:  xiechengguang
 * @date:  2017年6月6日 下午2:48:25
 *
 */
@Service
public class AppFileServiceImpl implements AppFileService {
	@Autowired private ProtocolSelector protocolSelector;
	@Autowired private FileRepository fileRepository;
	
	public void saveFile(File file){
		fileRepository.save(file);
	}
	
	
	/**
	 * <p>Title: uploadFile</p> 
	 * <p>Description: 文件上传处理方法</p> 
	 * @param requestProtocol
	 * @return 
	 * @see com.fire.control.system.module.file.service.AppFileService#uploadFile(com.fire.control.system.support.protocol.app.AppRequestProtocol)
	 */
	@Override
	public ResponseProtocol uploadFile(MultipartHttpServletRequest request) {
		
		ResponseProtocol respProtocol = protocolSelector.choose(new AppRequestProtocol());
		
		List<AppFileVo> fileVos = new ArrayList<AppFileVo>(); 
		MultiValueMap<String, MultipartFile> map = request.getMultiFileMap();  // 为了获取文件，这个类是必须的 
		
		for (String key : map.keySet()) {
			
			List<MultipartFile> list = map.get(key);
			
			if(list != null && list.isEmpty() == false) {
	        	// 将图片进行存储 
	            for (MultipartFile mFile : list) { 
	            	String originalFileName= mFile.getOriginalFilename();//获取文件名称（含后缀名）
	            	if(originalFileName != null) {
	            		/*String suffixName = originalFileName.substring(originalFileName.lastIndexOf(".")+1); //后缀名
	            		if(!("jpg".equals(suffixName) || "JPG".equals(suffixName) || "jpeg".equals(suffixName) || "JPEG".equals(suffixName) || 
	            		   "png".equals(suffixName) || "PNG".equals(suffixName) || "gif".equals(suffixName) || "GIF".equals(suffixName))) {
	                		continue;
	                	}*/
	            		File file = FileUtil.uploadFile(mFile);
	            		File savedFile = fileRepository.save(file); // 保存文件到数据库
	            		
	            		if(savedFile != null) {
	            			AppFileVo vo = new AppFileVo();
	                		vo.setId(savedFile.getId());
	                		vo.setUrl(savedFile.getAccessAddress()); //最终应该是getAccessAddress()
	                		fileVos.add(vo);
	            		}
	            	}
	            }
	        }
			
		}
		
		if(fileVos != null && fileVos.isEmpty() == false) {
			ResponseMessage.CODE_200.messageToProtocol(respProtocol);
			respProtocol.setDate(new XdaoDate());
			respProtocol.setData(fileVos);	
			return respProtocol;
		}
		
		
		
		
        //List<MultipartFile> list = map.get("file");  // 获取到文件的列表 
	    
        
        ResponseMessage.CODE_400.messageToProtocol(respProtocol);
		respProtocol.setDate(new XdaoDate());
		return respProtocol;
        
	}
	
	

}
